package com.le.tester.imooc.masterJUC.threadlocal;

/**
 * 描述：threadLocal空指针，如果可以不用threadLocal来解决问题，还是不要使用ThreadLocal来解决问题
 */
public class ThreadLocalNPE {

    private ThreadLocal<Long> longThreadLocal = new ThreadLocal<>();

    //设置线程的id值
    public void set() {
        longThreadLocal.set(Thread.currentThread().getId());
    }
//long类型，返回的是数值，没有，Long是对象，判空可用
    public Long get() {
        //把Long中的null.转成long基本类型，本身就是不行的，get本身是T
        return longThreadLocal.get();
    }

    public static void main(String[] args) {
        ThreadLocalNPE threadLocalNPE = new ThreadLocalNPE();
        //没有设置初始值，使用了get
        System.out.println(threadLocalNPE.get());
        new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocalNPE.set();
                System.out.println("新建线程的id is " + threadLocalNPE.get());
            }
        }).start();
    }
}
